<?php
include('../../Connections/KRAPPERCON.php');
//$ubigeo = '010101';

$dep = '';
$prov = '';
$ciu = '';
if (isset($ubigeo) and trim($ubigeo) !=''){
//echo 'UBIGEO:'.$ubigeo;
//echo $ubigeo."/".$ug_dep."/".$ug_prv;

// Cargar el Combo con la direccion enviada
	if ($ubigeo != ''){
	$ug_dep = substr($ubigeo,0,2);
	$ug_prv = substr($ubigeo,0,4);
	$ug_ciu = substr($ubigeo,0,6);
	
	$sqlx = 'select ubigeoid,ubigeotype,ubigeotxt from tt_ubigeo order by ubigeotype;';
	$resx = mysql_query($sqlx) or die('Error SQL');;
	$combo_dep = "<select id='cb_depa' onclick='loadProvincias(this)'>";
	$combo_prv = "<select id='cb_prov' onclick='loadCiudades(this)'>";
	$combo_ciu = "<select id='cb_ubigeo'>";
	
		while( $row = mysql_fetch_assoc($resx) ){
			$sel = '';
			//echo $row['ubigeotype'];
			if ($row['ubigeotype'] == 1){
				if ($row['ubigeoid'] == $ug_dep) $sel = " selected='selected' ";
				$combo_dep = $combo_dep."<option ".$sel." value='".$row['ubigeoid']."'>".$row['ubigeotxt']."</option>";
			}
			else if ($row['ubigeotype'] == 2 ){
				if ($row['ubigeoid'] == $ug_prv) $sel = " selected='selected' ";
				$combo_prv = $combo_prv."<option ".$sel." value='".$row['ubigeoid']."'>".$row['ubigeotxt']."</option>";
			}
			else if ($row['ubigeotype'] == 3 ){
				if ($row['ubigeoid'] == $ug_ciu) $sel = " selected='selected' ";
				$combo_ciu = $combo_ciu."<option ".$sel."value='".$row['ubigeoid']."'>".$row['ubigeotxt']."</option>";
			}
		}
	$combo_dep = $combo_dep."</select>";
	$combo_prv = $combo_prv."</select>";
	$combo_ciu = $combo_ciu."</select>";
	}
echo "<span>".$combo_dep."</span>"	;
echo "<span id='sp_provin'>".$combo_prv."</span>";
echo "<span id='sp_ciudad'>".$combo_ciu."</span>";
exit;
}



//------------------------------------------------------------
// Carga Normal de Ubicaciones
//------------------------------------------------------------
if (isset($_POST['depart'])) {
// Muestra Provincias por Departamento
include('../../Connections/KRAPPERCON.php');
$dep = $_POST['depart'];
$sql = "select UBIGEOID, UBIGEOTXT from TT_Ubigeo WHERE UBIGEOTOP = '".$dep."'";
$res = mysql_query($sql);
echo "<select id='cb_prov' onclick='loadCiudades(this)'>";
while( $row = mysql_fetch_assoc($res)) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
echo "</select>";
exit;
}

if (isset($_POST['provin'])) {
// Muestra Ciudades por provincia
include('../../Connections/KRAPPERCON.php');
$prv = $_POST['provin'];
$sql = "select UBIGEOID, UBIGEOTXT from TT_Ubigeo WHERE UBIGEOTOP = '".$prv."'";
$res = mysql_query($sql);
echo "<select id='cb_ubigeo' >";
while( $row = mysql_fetch_assoc($res)) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
echo "</select>";
exit;
}

?>
<span>
<?php 
// Lista todos los Departamentos
$sql = 'select UBIGEOID, UBIGEOTXT from TT_Ubigeo WHERE UBIGEOTYPE = 1';
$res = mysql_query($sql);
echo "<select id='cb_depa' onclick='loadProvincias(this)'>";
while( $row = mysql_fetch_assoc($res)) {
echo "<option value='".$row['UBIGEOID']."'>".$row['UBIGEOTXT']."</option>";
}
echo "</select>";
//mysql_close($db);
?>
</span>
<span id="sp_provin"></span>
<span id="sp_ciudad"></span>